package logic

import (
	"context"
	"fmt"
	"gitcode.com/sunsungk9/opensource-demo-cockroachdb-obs-241123/user/internal/svc"
	"gitcode.com/sunsungk9/opensource-demo-cockroachdb-obs-241123/user/internal/types"
	"github.com/zeromicro/go-zero/core/logx"
)

type BackupsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBackupsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackupsLogic {
	return &BackupsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BackupsLogic) Backups() (resp *types.BackupsResponse, err error) {
	paths := make([]string, 0)
	query := fmt.Sprintf("SHOW BACKUPS IN 's3://%s?AWS_ENDPOINT=%s&AWS_ACCESS_KEY_ID=%s&AWS_SECRET_ACCESS_KEY=%s'",
		l.svcCtx.Config.Bucket, l.svcCtx.Config.Endpoint, l.svcCtx.Config.AK, l.svcCtx.Config.SK)
	err = l.svcCtx.DB.SQL(query).Find(&paths)
	if err != nil {
		return nil, err
	}
	resp = new(types.BackupsResponse)
	resp.Message = "success"
	resp.Data = paths
	return
}
